package com.sso.controller;

import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Desc:
 * @ProjectName： sa-sso1-client
 * @Author： xhs
 * @Date： 2021/12/6 15:14
 * @Version: JDK 1.8
 */
@RestController
public class SsoClientController {

    /**
     * SSO-Client端：首页
     *
     * @return
     */
    @RequestMapping("/")
    public String index() {
        String authUrl = SaManager.getConfig().getSso().getAuthUrl();
        String solUrl = SaManager.getConfig().getSso().getSloUrl();
        String str = "<h2>Sa-Token SSO-Client 应用端</h2>" +
                "<p>当前会话是否登录：" + StpUtil.isLogin() + "</p>" +
                "<p><a href=\"javascript:location.href='" + authUrl + "?mode=simple&redirect=' + encodeURIComponent(location.href);\">登录</a> " +
                "<a href=\"javascript:location.href='" + solUrl + "?back=' + encodeURIComponent(location.href);\">注销</a> </p>";
        return str;
    }

    /**
     * 全局异常拦截
     *
     * @param e
     * @return
     */
    @ExceptionHandler
    public SaResult handlerException(Exception e) {
        e.printStackTrace();
        return SaResult.error(e.getMessage());
    }
}
